/**
* Copyright (C)2001-2099 Zeai.cn All rights reserved.
* E-Mail：supdes#qq.com　QQ:797311
* http://www.zeai.cn
* http://www.yzlove.com
* Last update 2020/09/01 by supdes
*/
$(function(){
	window.topadvs_time=null;
	//向左
	function topadv_goleft(){
		var tt = arguments[0] ? arguments[0] :300;
		var W=parseInt(screen.width);
		window.clearTimeout(window.topadvs_time);
		$(".topadvs_li:eq(0)").stop().animate({marginLeft:-W+"px"},tt,"swing",function(){	
			$(".topadvs_li:eq(0)").appendTo(".topadvs_main").css("margin-left","0px");
			var now_syh=parseInt($(".topadvs_li:eq(0)").attr("bj"));
			$(".topadvs_ico_li").removeClass("topadvs_ico_li_on");
			$(".topadvs_ico_li:eq("+now_syh+")").addClass("topadvs_ico_li_on");	
		});	
		topadvs_play();
	}
	//向右
	function topadv_goright(){	
		var tt = arguments[0] ? arguments[0] :300;
		var W=parseInt(screen.width);
		//最后一个移动到最前面，然后
		window.clearTimeout(window.topadvs_time);
		$(".topadvs_li:last").prependTo(".topadvs_main").css("margin-left","-"+W+"px");	
		$(".topadvs_li:eq(0)").stop().animate({marginLeft:"0px"},tt,"swing",function(){
			var now_syh=parseInt($(".topadvs_li:eq(0)").attr("bj"));	
			$(".topadvs_ico_li").removeClass("topadvs_ico_li_on");
			$(".topadvs_ico_li:eq("+now_syh+")").addClass("topadvs_ico_li_on");			
		});	
		topadvs_play();
	}
	function topadvs_play(){ window.topadvs_time=setTimeout(function(){ topadv_goleft();},4000);}
	function topadv_dian(sdx){
		var tt = arguments[0] ? arguments[0] :300;
		var syh=parseInt($(".topadvs_li:eq(0)").attr("bj"));
		var W=parseInt(screen.width);
		window.clearTimeout(window.topadvs_time);	
		if(sdx!=syh){
			$(".topadvs_main").find(".topadvs_li").each(function(index, element) {
				if(parseInt($(this).attr("bj"))==parseInt(sdx)){
					var mW=W*index;
					var objs=$(".topadvs_li:lt("+index+")");
					$(".topadvs_li:lt("+index+")").stop().animate({marginLeft:-mW+"px"},tt,"swing",function(){objs.appendTo(".topadvs_main").css("margin-left","0px");});
					$(".topadvs_ico_li").removeClass("topadvs_ico_li_on");
					$(".topadvs_ico_li:eq("+sdx+")").addClass("topadvs_ico_li_on");					
					topadvs_play();		
					return false;
				}
			});	
		}else{			
			$(".topadvs_ico_li").removeClass("topadvs_ico_li_on");
			$(".topadvs_ico_li:eq("+sdx+")").addClass("topadvs_ico_li_on");			
			topadvs_play();
		}
	}
	//触摸
	function topadvs_touch() {
		var _start = 0, _end = 0, _content = o("topadvs");
		_content.addEventListener("touchstart", touchStart, false);
		_content.addEventListener("touchmove", touchMove, false);
		_content.addEventListener("touchend", touchEnd, false);
		function touchStart(event) {
			var touch = event.targetTouches[0];
			_start = touch.pageX;
		}
		function touchMove(event) {
			var touch = event.targetTouches[0];
			_end = (_start - touch.pageX);
		}
		function touchEnd(event) {
			if (_end < -60) {
				topadv_goright(100);
				_end=0;
			}else if(_end > 60){
				topadv_goleft(100);
				_end=0;
			}
		}
	}
	function topadvs_int(){
		var W=parseInt(screen.width);
		var H=442;		
		$('#topadvs_ico').css({"top":(H-50)+'px'});
		$('#topadvs').css({"width":W+'px',"height":H+'px'});
		$('.topadvs_li').css({"width":W+'px',"height":H+'px'});		
		var topadvs_html='';
		$(".topadvs_main").find("div").each(function(index, element) {
			$(this).attr("bj",index);$(".topadvs_ico").append('<span id="topadvs_ico_li'+index+'" class="topadvs_ico_li" bj="'+index+'"></span>');
			$('#topadvs_ico_li'+index).on("click",function(){ var sdx=$(this).attr("bj");topadv_dian(sdx);});
		});				
		$(".topadvs_main").append($(".topadvs_main").html());
		var now_syh=parseInt($(".topadvs_li:eq(0)").attr("bj"));
		$(".topadvs_ico_li").removeClass("topadvs_ico_li_on");
		$(".topadvs_ico_li:eq("+now_syh+")").addClass("topadvs_ico_li_on");
		topadvs_touch();
		topadvs_play();
		$("#topadvs_ico").height(30);
		$('#prev').on("click",function(){topadv_goleft();});
		$('#next').on("click",function(){topadv_goright();});
	}
	topadvs_int();
});